package com.ziroom.zifamily.zfms.common.intercepter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.ziroom.zifamily.zfms.mapper.base.OperationLogEntityMapper;
import com.ziroom.zifamily.zfms.mapper.base.entity.OperationLogEntity;
import com.ziroom.zifamily.zfms.common.util.UserUtils;
import com.ziroom.zifamily.zfms.pojo.obj.UserInfo;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * <p></p>
 * <p>
 * <PRE>
 * <BR>    修改记录
 * <BR>-----------------------------------------------
 * <BR>    修改日期         修改人          修改内容
 * </PRE>
 *
 * @author xinggh
 * @version 1.0
 * @date Created in 2018年11月24日 19:08
 * @since 1.0
 */
public class OperationLogInterceptor implements HandlerInterceptor {
    /**
     * 进入controller方法之前
     */
    @Override
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response, Object handler) throws Exception {
        OperationLogEntity logEntity = new OperationLogEntity();
        //获取请求sessionId
        String sessionId = request.getRequestedSessionId();
        //请求路径
        String url = request.getRequestURI();
        //获取请求参数信息
        String paramData = JSON.toJSONString(request.getParameterMap(),
                SerializerFeature.DisableCircularReferenceDetect,
                SerializerFeature.WriteMapNullValue);
        UserInfo userInfo = UserUtils.getUserInfo();
        //设置请求访问员工号
        logEntity.setEmpCode(userInfo.getSystemCode());
        //设置请求访问员工姓名
        logEntity.setEmpName(userInfo.getNickName());

        logEntity.setOpMenuUrl(url);
        //设置请求方法
        logEntity.setOpMenuName(request.getMethod());
        request.setAttribute("LOGGER_ENTITY",logEntity);

        return true;
    }

    /**
     * 调用完controller之后，视图渲染之前
     */
    @Override
    public void postHandle(HttpServletRequest request,
                           HttpServletResponse response, Object handler,
                           ModelAndView modelAndView) throws Exception {

    }

    /**
     * 整个完成之后，通常用于资源清理
     */
    @Override
    public void afterCompletion(HttpServletRequest request,
                                HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
        //获取请求错误码
        int status = response.getStatus();
        //当前时间
        long currentTime = System.currentTimeMillis();

        //获取本次请求日志实体
//        OperationLogEntity loggerEntity = (OperationLogEntity) request.getAttribute("LOGGER_ENTITY");
//        OperationLogEntityMapper mapper = getDAO(OperationLogEntityMapper.class,request);
//        mapper.saveOperationLog(loggerEntity);
    }

    /**
     * 根据传入的类型获取spring管理的对应dao
     * @param clazz 类型
     * @param request 请求对象
     * @param <T>
     * @return
     */
    private <T> T getDAO(Class<T> clazz,HttpServletRequest request)
    {
        BeanFactory factory = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getServletContext());
        return factory.getBean(clazz);
    }

}
